﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SemiCASE.Business;

public partial class PopUp_CreateUser : SemiCASEBasePage
{
    private string UserID
    {
        get
        {
            return Request.QueryString["UserID"] != null ? Request.QueryString["UserID"] : String.Empty;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!String.IsNullOrEmpty(this.UserID))
                LoadUserInfo();

        }
    }

    private void LoadUserInfo()
    {
        if (String.IsNullOrEmpty(this.UserID))
            return;

        DataTable dt = new LoginBS().GetUserInfo(new Guid(this.UserID));

        if (dt.Rows.Count == 0)
            return;

        DataRow row = dt.Rows[0];
        TextBoxUserName.Text = (row["UserName"] != DBNull.Value) ? row["UserName"].ToString() : String.Empty;
        TextBoxPassword.Text = (row["Password"] != DBNull.Value) ? row["Password"].ToString() : String.Empty;
        GenelTextBoxName.Text = (row["Name"] != DBNull.Value) ? row["Name"].ToString() : String.Empty;
        GenelTextBoxSurname.Text = (row["Surname"] != DBNull.Value) ? row["Surname"].ToString() : String.Empty;
        GenelTextBoxEmail.Text = (row["Email"] != DBNull.Value) ? row["Email"].ToString() : String.Empty;
        if (row["IsAdmin"] != DBNull.Value)
        {
            CheckBoxIsAdmin.Checked = Convert.ToBoolean(row["IsAdmin"].ToString());
        }
    }

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (!String.IsNullOrEmpty(this.UserID))
            UpdateUser();
        else
            SaveUser();
    }

    private void SaveUser()
    {
        if (!ValidateEnteredUserInfo())
            return;

        new LoginBS().InsertUser(TextBoxUserName.Text, TextBoxPassword.Text, CheckBoxIsAdmin.Checked, GenelTextBoxName.Text, GenelTextBoxSurname.Text, GenelTextBoxEmail.Text);        
        JavascriptHelper.PopUpiKapatAcanPencereyiRefresh();
    }

    private void UpdateUser()
    {
        if (!ValidateEnteredUserInfo())
            return;

        new LoginBS().UpdateUser(new Guid(this.UserID), TextBoxUserName.Text, TextBoxPassword.Text, CheckBoxIsAdmin.Checked, GenelTextBoxName.Text, GenelTextBoxSurname.Text, GenelTextBoxEmail.Text);        
        JavascriptHelper.PopUpiKapatAcanPencereyiRefresh();
    }

    private bool ValidateEnteredUserInfo()
    {
        if (String.IsNullOrEmpty(TextBoxUserName.Text))
        {
            MessageBox("Please enter User name", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return false;
        }

        if (String.IsNullOrEmpty(TextBoxPassword.Text))
        {
            MessageBox("Please enter password", SemiCASE.Interfaces.MesajTuruEnum.Uyari);
            return false;
        }

        return true;
    }
}